Skip to content

fix(core): consolidate ID generation to prevent HTTP self-hosted crashes#3977

Merged
waleedlatif1 merged 4 commits intostagingfrom
waleedlatif1/uuid-fallback
Apr 5, 2026
Merged

fix(core): consolidate ID generation to prevent HTTP self-hosted crashes#3977
waleedlatif1 merged 4 commits intostagingfrom
waleedlatif1/uuid-fallback

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

Summary

  • crypto.randomUUID() requires HTTPS/secure context in browsers — crashes on self-hosted HTTP deployments with white screen
  • Added central generateId() utility in @/lib/core/utils/uuid that falls back to crypto.getRandomValues() (works everywhere)
  • Replaced all crypto.randomUUID(), import { randomUUID } from 'crypto', nanoid, and uuid package usage across ~320 files
  • Added generateShortId() (replaces nanoid) and isValidUuid() (replaces uuid validate)
  • Removed nanoid dependency from apps/sim and packages/testing
  • Removed browser polyfill script from layout.tsx
  • Updated test mocks, CLAUDE.md, AGENTS.md, cursor rules

Type of Change

  • Bug fix

Testing

  • TypeScript compiles clean
  • Lint passes
  • Verified zero remaining crypto.randomUUID(), nanoid, or uuid imports in apps/sim/

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

crypto.randomUUID() requires a secure context (HTTPS) in browsers,
causing white-screen crashes on self-hosted HTTP deployments. This
replaces all direct usage of crypto.randomUUID(), nanoid, and the uuid
package with a central utility that falls back to crypto.getRandomValues()
which works in all contexts.

- Add generateId(), generateShortId(), isValidUuid() in @/lib/core/utils/uuid
- Replace crypto.randomUUID() imports across ~220 server + client files
- Replace nanoid imports with generateShortId()
- Replace uuid package validate with isValidUuid()
- Remove nanoid dependency from apps/sim and packages/testing
- Remove browser polyfill script from layout.tsx
- Update test mocks to target @/lib/core/utils/uuid
- Update CLAUDE.md, AGENTS.md, cursor rules, claude rules

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 5, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Apr 5, 2026 6:17pm

Request Review

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 5, 2026

PR Summary

Medium Risk
Broad, cross-cutting refactor touching many API routes and ID/token generation paths; while intended to be behavior-preserving, any mismatch in ID format/uniqueness or missing import could impact persistence, locking, or auth flows.

Overview
Standardizes ID creation across the codebase by introducing @/lib/core/utils/uuid (generateId, generateShortId, isValidUuid) to avoid crypto.randomUUID() secure-context crashes and replace direct usage of uuid/nanoid.

Updates a large set of API routes to use the new helpers for record IDs, request IDs, tokens, lock values, and message/run/execution IDs, and adjusts tests/mocks plus developer guidelines (AGENTS.md, CLAUDE.md, and editor rules) to enforce the new convention.

Reviewed by Cursor Bugbot for commit 7be21d2. Configure here.

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 5, 2026

Greptile Summary

This PR consolidates all ID generation to a central @/lib/core/utils/uuid module, replacing crypto.randomUUID(), nanoid, and the uuid package across ~320 files. The core motivation — crypto.randomUUID() is unavailable in non-secure (HTTP) browser contexts, causing white-screen crashes on self-hosted deployments — is correctly addressed by the new generateId() utility, which falls back to crypto.getRandomValues() (universally available).

Key changes:

  • New generateId() (UUID v4, secure-context-safe), generateShortId() (nanoid replacement, 64-char alphabet, & 63 mask — correct), and isValidUuid() (replaces uuid.validate) in apps/sim/lib/core/utils/uuid.ts
  • Mass migration of all call sites to use the new utilities via absolute imports
  • nanoid removed from apps/sim/package.json and packages/testing/package.json
  • Browser polyfill script removed from layout.tsx
  • packages/testing/src/mocks/uuid.mock.ts emptied to a comment (deprecated vi.doMock-based helpers removed), directing tests to use vi.hoisted() + vi.mock() directly
  • Test files updated to mock @/lib/core/utils/uuid using the correct pattern

Issue found: The uuid package (^11.1.0) is still declared as a production dependency in apps/sim/package.json (line 193) even though no file imports from it after this change — an incomplete cleanup alongside the nanoid removal.

Confidence Score: 4/5

PR is safe to merge with one cleanup item: the orphaned uuid dependency in package.json should be removed before merging.

The core fix is correct and well-implemented. The UUID v4 fallback logic is mathematically sound, the nanoid-compatible alphabet is exact, and test mocks follow project conventions. One P1 remains: the uuid package is still declared as a production dependency despite having zero consumers — a straightforward line deletion that prevents dead dependency bloat.

apps/sim/package.json line 193 — remove the orphaned uuid dependency.

Important Files Changed

Filename Overview
apps/sim/lib/core/utils/uuid.ts New central UUID utility — implementation is correct (proper UUID v4 bit-setting, 64-char nanoid-compatible alphabet); minor: fallback path lacks an explicit guard if crypto is completely absent
apps/sim/package.json nanoid correctly removed, but uuid package (^11.1.0) is still listed as a production dependency despite zero remaining consumers
packages/testing/src/mocks/uuid.mock.ts Emptied to a comment pointing devs to the correct vi.hoisted() + vi.mock() pattern; deprecated helpers removed
packages/testing/src/factories/id.ts New shortId() factory using crypto.getRandomValues() consistent with app-level generateShortId()
apps/sim/app/layout.tsx Browser polyfill script for crypto.randomUUID removed; existing workspace-layout-dimensions script is unrelated and correctly retained
packages/testing/package.json nanoid dependency removed cleanly

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["ID generation call site"] --> B{"generateId() or generateShortId()"}
    B --> C["apps/sim/lib/core/utils/uuid.ts"]
    C --> D{"crypto.randomUUID\navailable?"}
    D -->|"Yes (HTTPS or\nNode.js 20+)"| E["crypto.randomUUID()\n→ UUID v4"]
    D -->|"No (HTTP browser,\nnon-secure context)"| F["crypto.getRandomValues()\n→ manual UUID v4"]
    C --> G{"generateShortId()"}
    G --> H["crypto.getRandomValues()\n→ 64-char alphabet"]
    E --> I["UUID string"]
    F --> I
    H --> J["Short URL-safe ID"]
    style C fill:#4ade80,stroke:#16a34a
    style D fill:#fbbf24,stroke:#d97706
Loading

Reviews (2): Last reviewed commit: "fix(core): remove deprecated uuid mock h..." | Re-trigger Greptile

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit cf15265. Configure here.

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 7be21d2. Configure here.

@waleedlatif1 waleedlatif1 merged commit a680cec into staging Apr 5, 2026
12 checks passed
@waleedlatif1 waleedlatif1 deleted the waleedlatif1/uuid-fallback branch April 5, 2026 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant